//
//  LBTransitionInteractivePopAnimation.m
//  TEXT
//
//  Created by mac on 2025/3/30.
//  Copyright © 2025 刘博. All rights reserved.
//

#import "LBTransitionInteractivePopLinearAnimation.h"
#import <UIKit/UIKit.h>
#import "UIView+LBFrame.h"

@interface LBTransitionInteractivePopLinearAnimation ()

@property (strong, nonatomic) id<UIViewControllerContextTransitioning> transitionContext;

@property (strong, nonatomic) UIView *containerView;

@property (strong, nonatomic) UIView *animationContainerView;

@end

@implementation LBTransitionInteractivePopLinearAnimation

- (instancetype)init{
    self = [super init];
    if (self == nil) return nil;
    
    return self;
}

#pragma mark - Transition

- (NSTimeInterval)transitionDuration {
    return 0.4;
}

- (void)prepareTransitionWithTransitionContext:(id<UIViewControllerContextTransitioning>)transitionContext {
    self.transitionContext = transitionContext;

    UIView *containerView = transitionContext.containerView;
    containerView.backgroundColor = UIColor.clearColor;

    self.containerView = containerView;

    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    toView.frame = containerView.bounds;
    toView.x = -(containerView.width / 4.0);
    
    [self.containerView addSubview:toView];
    
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    fromView.frame = containerView.bounds;
    
    [self.containerView addSubview:fromView];
}

- (BOOL)updateTransitionWithPercentComplete:(CGFloat)percentComplete translation:(CGPoint)translation {
    UIView *toView = [self.transitionContext viewForKey:UITransitionContextToViewKey];
    UIView *fromView = [self.transitionContext viewForKey:UITransitionContextFromViewKey];
    UIView *containerView = self.transitionContext.containerView;

    CGFloat toViewInitialX = containerView.x - (containerView.width / 4.0);
    CGFloat toViewMaxX = containerView.x;
    CGFloat fromViewInitialX = containerView.x;
    CGFloat fromViewMaxX = containerView.width;
    
    toView.x = containerView.x - (containerView.width / 4.0) + translation.x * 0.25;
    fromView.x = containerView.x + translation.x;
    
    if (toView.x < toViewInitialX) {
        toView.x = toViewInitialX;
    }
    
    if (toView.x > toViewMaxX) {
        toView.x = toViewMaxX;
    }
    
    if (fromView.x < fromViewInitialX) {
        fromView.x = fromViewInitialX;
    }
    
    if (fromView.x > fromViewMaxX) {
        fromView.x = fromViewMaxX;
    }
    
    [self.transitionContext updateInteractiveTransition:percentComplete];
    
    return NO;
}

- (void)finishTransitionIsCancel:(BOOL)isCancel completion:(LIVDetailTransitionAnimationCompletion)completion {
    UIView *toView = [self.transitionContext viewForKey:UITransitionContextToViewKey];
    UIView *fromView = [self.transitionContext viewForKey:UITransitionContextFromViewKey];
    UIView *containerView = self.transitionContext.containerView;

    CGFloat toViewInitialX = containerView.x - (containerView.width / 4.0);
    CGFloat toViewMaxX = containerView.x;
    CGFloat fromViewInitialX = containerView.x;
    CGFloat fromViewMaxX = containerView.width;

    [UIView animateWithDuration:[self transitionDuration] delay:0.0 usingSpringWithDamping:1.0 initialSpringVelocity:0.0 options:UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionCurveLinear animations:^{
        if (isCancel) {
            toView.x = toViewInitialX;
            fromView.x = fromViewInitialX;
        } else {
            toView.x = toViewMaxX;
            fromView.x = fromViewMaxX;
        }
    } completion:^(BOOL finished) {
        NSLog(@"interactive pop linear animation completion: %d, %d", finished, isCancel);

        if (completion != nil) {
            completion(finished, isCancel, self.transitionContext);
        }
    }];
}

@end
